<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\User;
use Dcat\Admin\Controllers\AdminController;
use Dcat\Admin\IFrameGrid;
use Dcat\Admin\Models\Administrator;
use Illuminate\Support\Facades\Auth;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;

class UserController extends AdminController
{
    protected function iFrameGrid()
    {
        $grid = new IFrameGrid(new Administrator());

        $grid->rowSelector()->titleColumn('name');

        $grid->id->sortable();
        $grid->name;

        $grid->disableEditButton();
        $grid->disableDeleteButton();
        $grid->disableBatchDelete();

        $grid->filter(function (Grid\Filter $filter) {
            $filter->equal('id');
            $filter->like('name');
        });

        return $grid;
    }

    protected function grid()
    {
        return Grid::make(
            new User(['updateAdmin', 'createAdmin']),
            function (Grid $grid) {
                $grid->id;
                $grid->name;
                $grid->email;
                $grid->mobile;
                $grid->gender->display(function ($value) {
                    return $value ? '男' : '女';
                });
                $grid->address->display(function ($value) {
                    return $value ? $value['province'] . $value['city'] . $value['area'] . $value['address'] : "未填写";
                });
                $grid->status->display(function ($value) {
                    return $value ? '是' : '否';
                });
                $grid->column('update_admin.username', '负责管理员');
                $grid->column('create_admin.username', '修改管理员');
                $grid->created_at;
            }
        );
    }

    protected function detail($id)
    {
        return Show::make($id, new User(), function (Show $show) {
            $show->id;
            $show->name;
            $show->email;
            $show->mobile;
            $show->password;
            $show->gender;
            $show->address;
            $show->status;
            $show->create_admin_id;
            $show->update_admin_id;
            $show->created_at;
            $show->updated_at;
        });
    }

    protected function form()
    {
        return Form::make(new User(), function (Form $form) {
            $form->display('id');
            $form->text('name')->required();
            $form->select('type', '用户类型')->options(\App\Models\User::$typeMap)->default(\App\Models\User::TYPE_DEFAULT);
            $form->email('email');
            $form->mobile('mobile')->required();
            $form->password('password')->required()->default('123456')->help('默认密码为：123456');
            $form->radio('gender')->options([0 => '男', 1 => '女'])->default(0);
            $form->embeds('address', function ($form) {
                $form->text('province', '省');
                $form->text('city', '市');
                $form->text('area', '区');
                $form->text('address', '详细地址');
            });
            $form->radio('status')->options([0 => '关闭', 1 => '开启'])->default(1);
            $form->hidden('create_admin_id')->value(Auth::guard('admin')->id());
            $form->hidden('update_admin_id')->value(Auth::guard('admin')->id());


            $form->saving(function (Form $form) {
                $form->password = bcrypt($form->password);
            });
        });
    }

}
